package com.ktgj.customer.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ktgj.customer.domain.*;
import com.ktgj.customer.domain.dto.PositionDetailsDTO;
import com.ktgj.customer.domain.dto.PositionSelectDTO;
import com.ktgj.customer.domain.dto.SysMenuAllDTO;
import com.ktgj.customer.domain.dto.SysMenuLoginDTO;
import com.ktgj.customer.domain.vo.PositionVO;
import com.ktgj.customer.mapper.MenuMapper;
import com.ktgj.customer.mapper.PositionMenuMapper;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.apache.poi.ss.usermodel.ExcelStyleDateFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.PositionMapper;
import com.ktgj.customer.service.IPositionService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 职位Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-27
 */
@Service
public class PositionServiceImpl implements IPositionService {

    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private MenuServiceImpl menuService;

    @Autowired
    private PositionMenuMapper positionMenuMapper;

    @Resource
    private MenuMapper menuMapper;

    /**
     * 查询职位
     *
     * @param positionId 职位ID
     * @return 职位
     */
    @Override
    public PositionDTO selectPositionById(Long positionId) {
        return positionMapper.selectPositionById(positionId);
    }

    /**
     * 查询职位列表
     *
     * @param position 职位
     * @return 职位
     */
    @Override
    public List<Position> selectPositionList(Position position) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Position> positions = positionMapper.selectPositionList(position);
        if (positions != null && positions.size() > 0)
        {
            for (Position dto : positions)
            {
                if (dto.getCreateTime() != null)
                {
                    dto.setCreateDate(format.format(dto.getCreateTime()));
                }

                List<Long> permissionList = new ArrayList<>();
                //权限
                if (dto.getPermission() != 0)
                {
                    Long i = dto.getPermission();
                    //包含审核（或其他）
                    if (i > 16)
                    {
                        permissionList.add(16l);
                        i = i - 16;
                    }

                    //包含提交（或申请）
                    if (i > 8)
                    {
                        permissionList.add(8l);
                        i = i - 8;
                    }

                    //包含删除
                    if (i > 4)
                    {
                        permissionList.add(4l);
                        i = i - 4;
                    }

                    //包含编辑
                    if (i > 2)
                    {
                        permissionList.add(2l);
                        i = i - 2;
                    }

                    //包含浏览
                    if (i > 1)
                    {
                        permissionList.add(1l);
                    }

                    position.setPermissionList(permissionList);
                }

                List<String> positionMenuList = new ArrayList<>();
                List<PositionMenu> positionMenus = positionMenuMapper.menuIdList(dto.getPositionId());
                if (positionMenus != null && positionMenus.size() > 0)
                {
                    List<MenuDTO> lstAllMenu = new ArrayList<>();
                    for (PositionMenu positionMenu : positionMenus)
                    {
                        positionMenuList.add(positionMenu.getMenuId().toString());
                        MenuDTO m = menuMapper.selectMenuById(positionMenu.getMenuId());
                        if (m!=null)
                        {
                            lstAllMenu.add(m);
                        }
                    }
                    for (MenuDTO rootMenu : lstAllMenu)
                    {
                        if (rootMenu == null)
                        {
                            continue;
                        }
                        if (rootMenu.getMenuParent() == null)
                        {
                            dto.getLstMenu().add(rootMenu);
                            getChildMenu(rootMenu, lstAllMenu);
                        }

                    }
                    dto.setPositionMenuList(positionMenuList);
                }
            }
        }
        return positions;
    }

    private void getChildMenu(MenuDTO menu, List<MenuDTO> lstMenu)
    {
        for (MenuDTO m : lstMenu)
        {
            if (m == null)
            {
                continue;
            }
            if (m.getMenuParent() != null)
            {
                if (m.getMenuParent().equals(menu.getMenuId()))
                {
                    menu.getChildren().add(m);
                    getChildMenu(m, lstMenu);
                }
            }
        }
    }

    /**
     * 查询职位列表
     *
     * @return 职位集合
     */
    @Override
    public List<Position> queryPositionList() {
        return positionMapper.queryPositionList();
    }

    /**
     * 新增职位
     *
     * @param positionVO 职位
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPosition(PositionVO positionVO) {
        Date date = new Date();
        Long permission = 0L;
        for (Long item : positionVO.getPermissions())
            permission = permission + item;
        positionVO.setPermission(permission);
        positionVO.setCreateTime(date);
        positionVO.setEnableStart("true");
        int i = positionMapper.insertPosition(positionVO);
        if (i > 0) {
            List<PositionMenu> positionMenus = new ArrayList<>();
            for (Long item : positionVO.getMenus()) {
                if (item != 0) {
                    PositionMenu positionMenu = new PositionMenu();
                    positionMenu.setPositionId(positionVO.getPositionId());
                    positionMenu.setMenuId(item);
                    positionMenu.setPermission(positionVO.getPermission());
                    positionMenus.add(positionMenu);
                }
            }
            positionMenuMapper.deletePositionMenuByPositionId(positionVO.getPositionId());
            i = positionMenuMapper.insertBatch(positionMenus);
        }
        return i;
    }

    /**
     * 修改职位
     *
     * @param positionVO 职位
     * @return 结果
     */
    @Override
    public int updatePosition(PositionVO positionVO) {
        Long permission = 0L;
        for (Long item : positionVO.getPermissions())
            permission = permission + item;
        positionVO.setPermission(permission);
        int i = positionMapper.updatePosition(positionVO);

        if (i > 0) {
            PositionMenu positionMenu1=new PositionMenu();
            positionMenu1.setPositionId(positionVO.getPositionId());
            //查询职位菜单是否存在
            List<PositionMenu> positionMenuss=positionMenuMapper.selectPositionMenuList(positionMenu1);
            //存在先删除后添加
            if(positionMenuss.size()>0){
            i = positionMenuMapper.deletePositionMenuByPositionId(positionVO.getPositionId());
            if (i > 0) {
                List<PositionMenu> positionMenus = new ArrayList<>();
                for (Long item : positionVO.getMenus()) {
                    if (item != 0) {
                        PositionMenu positionMenu = new PositionMenu();
                        positionMenu.setPositionId(positionVO.getPositionId());
                        positionMenu.setMenuId(item);
                        positionMenu.setPermission(positionVO.getPermission());
                        positionMenus.add(positionMenu);
                    }
                }
                i = positionMenuMapper.insertBatch(positionMenus);
            }
            }else {
                //不存在直接添加
                List<PositionMenu> positionMenus = new ArrayList<>();
                for (Long item : positionVO.getMenus()) {
                    if (item != 0) {
                        PositionMenu positionMenu = new PositionMenu();
                        positionMenu.setPositionId(positionVO.getPositionId());
                        positionMenu.setMenuId(item);
                        positionMenu.setPermission(positionVO.getPermission());
                        positionMenus.add(positionMenu);
                    }
                }
                i = positionMenuMapper.insertBatch(positionMenus);
            }
        }

        return i;
    }

    /**
     * 批量删除职位
     *
     * @param positionIds 需要删除的职位ID
     * @return 结果
     */
    @Override
    public int deletePositionByIds(Long[] positionIds) {
        return positionMapper.deletePositionByIds(positionIds);
    }

    /**
     * 删除职位信息
     *
     * @param positionId 职位ID
     * @return 结果
     */
    @Override
    public int deletePositionById(Long positionId) {
        return positionMapper.deletePositionById(positionId);
    }

    /**
     * 查询用户的所有菜单ID
     */
    @Override
    public List<Long> queryAllMenuId(Long userId) {
        // TODO
        return null;
    }

    /**
     * 根据职位id查询菜单
     *
     * @param positionId
     * @return
     */
    @Override
    public PositionDTO queryMenuTree(Long positionId) {
        PositionDTO positionDTO = new PositionDTO();
        List<MenuDTO> menuDTOS = menuService.menuListTree();
        positionDTO.setMenuDTO(menuDTOS);
        return positionDTO;
    }

    @Override
    public List<PositionSelectDTO> getAll(PositionSelectDTO positionSelectDTO) {
        return positionMapper.getAll(positionSelectDTO);
    }

    @Override
    public PositionDetailsDTO queryPositionById(Long positionId) {
        PositionDetailsDTO positionDetailsDTO = positionMapper.queryPositionById(positionId);

        MenuServiceImpl menuService = new MenuServiceImpl();
        SysMenuAllDTO sysMenuAllDTO = new SysMenuAllDTO();
        for (SysMenuLoginDTO item : positionDetailsDTO.getSysMenuLoginDTOS()) {

            List<SysMenuLoginDTO> menus = menuService.menuUtil(item.getCh());

            switch (item.getMenuName()) {
                case "system":
                    sysMenuAllDTO.setSystem(menus);
                    break;
                case "customer":
                    sysMenuAllDTO.setCustomer(menus);
                    break;
                case "flight":
                    sysMenuAllDTO.setFlight(menus);
                    break;
                case "VIPBoarding":
                    sysMenuAllDTO.setVIPBoarding(menus);
                    break;
                case "VIPHall":
                    sysMenuAllDTO.setVIPHall(menus);
                    break;
                case "VIPChannel":
                    sysMenuAllDTO.setVIPChannel(menus);
                    break;
                case "airportService":
                    sysMenuAllDTO.setAirportService(menus);
                    break;
                case "flightDelay":
                    sysMenuAllDTO.setFlightDelay(menus);
                    break;
                case "train":
                    sysMenuAllDTO.setTrain(menus);
                    break;
                case "hotel":
                    sysMenuAllDTO.setHotel(menus);
                    break;
                case "travel":
                    sysMenuAllDTO.setTravel(menus);
                    break;
                case "ticket":
                    sysMenuAllDTO.setTicket(menus);
                    break;
                case "specialCar":
                    sysMenuAllDTO.setSpecialCar(menus);
                    break;
                case "parking":
                    sysMenuAllDTO.setParking(menus);
                    break;
                case "DDDriving":
                    sysMenuAllDTO.setDDDriving(menus);
                    break;
                case "shopping":
                    sysMenuAllDTO.setShopping(menus);
                    break;
                case "healthy":
                    sysMenuAllDTO.setHealthy(menus);
                    break;
                case "insurance":
                    sysMenuAllDTO.setInsurance(menus);
                    break;
            }

        }

        positionDetailsDTO.setSysMenuAllDTO(sysMenuAllDTO);
        positionDetailsDTO.setSysMenuLoginDTOS(null);

        Long permission = positionDetailsDTO.getPermission();
        List<Integer> permissions = new ArrayList<>();
        if (permission == 1 || permission == 3 || permission == 5 || permission == 9 || permission == 17 || permission == 7 || permission == 11 || permission == 19 || permission == 13 || permission == 21 || permission == 25 || permission == 15 || permission == 23 || permission == 29 || permission == 31)
            permissions.add(1);
        if (permission == 2 || permission == 3 || permission == 6 || permission == 10 || permission == 18 || permission == 7 || permission == 11 || permission == 19 || permission == 14 || permission == 22 || permission == 26 || permission == 15 || permission == 23 || permission == 30 || permission == 31)
            permissions.add(2);
        if (permission == 4 || permission == 5 || permission == 6 || permission == 12 || permission == 20 || permission == 7 || permission == 13 || permission == 21 || permission == 14 || permission == 22 || permission == 28 || permission == 15 || permission == 23 || permission == 29 || permission == 30 || permission == 31)
            permissions.add(4);
        if (permission == 8 || permission == 9 || permission == 10 || permission == 12 || permission == 24 || permission == 11 || permission == 13 || permission == 25 || permission == 14 || permission == 26 || permission == 28 || permission == 15 || permission == 29 || permission == 30 || permission == 31)
            permissions.add(8);
        if (permission == 16 || permission == 17 || permission == 18 || permission == 20 || permission == 24 || permission == 19 || permission == 21 || permission == 25 || permission == 22 || permission == 26 || permission == 28 || permission == 23 || permission == 29 || permission == 30 || permission == 31)
            permissions.add(16);

        positionDetailsDTO.setPermissions(permissions);

        return positionDetailsDTO;
    }

    /**
     * 修改角色状态
     * */
    @Override
    public int toggleStart(PositionVO positionVO) {
        int i = positionMapper.updatePosition(positionVO);
        return i;
    }
}
